package cn.bossx.org.controller;

import cn.bossx.basic.util.AjaxResult;
import cn.bossx.basic.util.PageList;
import cn.bossx.org.domain.SystemDictionaryDetail;
import cn.bossx.org.query.SystemDictionaryDetailQuery;
import cn.bossx.org.service.ISystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryDetail")
@Api(tags = "数据字典明细接口",description = "数据字典明细接口详细描")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    //新增或修改   POST
    @PostMapping("/save")
    @ApiOperation(value = "数据字典添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail ){
        try {
            if (systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.add(systemDictionaryDetail);
            } else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }


    //删除    DELETE
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){

        try {
            systemDictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }
    }

    //获取一条数据   GET
    @GetMapping("/{id}")
    public SystemDictionaryDetail findById(@PathVariable("id") Long id){
        return systemDictionaryDetailService.queryById(id);
    }

    //获取所有数据   GET
    @GetMapping("/list")
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionaryDetailService.queryAll();
    }

    //分页 + 高级查询   POST
    @PostMapping("/page")
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery query){
        return systemDictionaryDetailService.queryPage(query);
    }

    /**
     * 批量删除
     *  如果前端传的是 数组,那么在后端可以使用 list 或者 数组 接收
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            systemDictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }

    }

    // 抽屉
    // 分页高级查询
    @PostMapping("/detail")
    public PageList<SystemDictionaryDetail> findDetailByTypeId(@RequestBody SystemDictionaryDetailQuery query){
        return systemDictionaryDetailService.findDetailByTypeId(query);
    }

}

